﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using DevTools.PopupKiller.Components;
using DevTools.Properties;

namespace DevTools.PopupKiller.Forms
{
    public partial class PopupKillerForm : Form
    {
        public PopupKillerForm()
        {
            InitializeComponent();

            notifyIcon.Icon = Resources.Delete;
        }

        private Killer _killer;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            LoadFromSetting();

            KeyboardHook hook = new KeyboardHook();
            hook.KeyUp += KeyboardHook_KeyUp;
        }

        private static void KeyboardHook_KeyUp(object sender, HookEventArgs e)
        {
            foreach (var target in ProcessKiller.Instance.Target)
                if (target.Control == e.Control)
                    if (target.Alt == e.Alt)
                        if (target.Shift == e.Shift)
                            if (char.ToUpper(target.Key) == e.Key.ToString()[0])
                                ProcessKiller.Instance.Kill(target.ProcessName);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Hide();

            e.Cancel = true;
        }

        private void LoadFromSetting()
        {
            txtPopCaption.Text = Settings.Default.PopupKiller_PopupCaptions;
            nupInterval.Value = Convert.ToDecimal(Settings.Default.PopupKiller_Interval);
        }

        private void SaveToSetting()
        {
            Settings.Default.PopupKiller_PopupCaptions = txtPopCaption.Text;
            Settings.Default.PopupKiller_Interval = Convert.ToDouble((decimal) nupInterval.Value);
            Settings.Default.Save();
        }

        private void RefreshMenu(bool startable)
        {
            mnuStart.Enabled = startable;
            mnuStop.Enabled = !startable;
            mnuStartMain.Enabled = startable;
            mnuStopMain.Enabled = !startable;

            if (startable)
            {
                slbLabel.Text = string.Empty;
            }
            else
            {
                slbLabel.Text = "Running...";

                notifyIcon.BalloonTipTitle = "Popup Killer is running...";
                notifyIcon.BalloonTipText = txtPopCaption.Text;
                notifyIcon.ShowBalloonTip(1000);
            }
        }

        private void Stop()
        {
            _killer.Stop();
        }

        private void Start()
        {
            var popupCaptions = txtPopCaption.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

            _killer = new Killer();
            _killer.Start(popupCaptions, Convert.ToDouble((decimal) nupInterval.Value));
        }

        private void Exit()
        {
            SaveToSetting();

            Application.Exit();
        }

        private void tsiFile_Exit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void tstHelp_About_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Resources.Arthor, Resources.Title + Resources.Version, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void mnuShow_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void mnuStart_Click(object sender, EventArgs e)
        {
            Start();
            RefreshMenu(false);
        }

        private void mnuStop_Click(object sender, EventArgs e)
        {
            Stop();
            RefreshMenu(true);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
        }

        private void mnuHelp_Click(object sender, EventArgs e)
        {
            Process.Start(@"http://kimgwajang.tistory.com/category/Dev%20Tools/팝업%20킬러");
        }

        private void mnuOption_Click(object sender, EventArgs e)
        {
            TargetForm form = new TargetForm();
            form.ShowDialog();
        }
    }
}